package com.example.pm.ending.server.controller;

import com.example.pm.ending.server.module.owner.AddNewOwnerModuleDTO;
import com.example.pm.ending.server.module.owner.OwnerListModuleVO;
import com.example.pm.ending.server.module.owner.OwnerStandardModuleVO;
import com.example.pm.ending.server.module.owner.OwnerUpdateModuleDTO;
import com.example.pm.ending.server.service.IOwnerModuleService;
import com.example.pm.ending.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/ownerModules")
@Api(tags = "1.业主管理模块")
public class OwnerModuleController {
    @Autowired
    private IOwnerModuleService ownerModuleService;

    @PostMapping("/addNewOwner")
    @ApiOperation("添加业主模块")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNewOwner(@Valid AddNewOwnerModuleDTO addNewOwnerModuleDTO) {
        ownerModuleService.addNewOwner(addNewOwnerModuleDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id}")
    @ApiOperation("删除业主信息")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "业主id", required = true)
    public JsonResult<Void> deleteOwner(@PathVariable("id") @Min(1) Long ownerId) {
        ownerModuleService.deleteOwnerById(ownerId);
        return JsonResult.ok();
    }

    @PostMapping("{ownerId}/enable")
    @ApiOperation("启用业主")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "ownerId", value = "业主id", required = true)
    public JsonResult<Void> enable(@PathVariable("ownerId") @Min(1) Long ownerId){
        ownerModuleService.ownerEnable(ownerId);
        return JsonResult.ok();
    }

    @PostMapping("{ownerId}/disEnable")
    @ApiOperation("禁用业主")
    @ApiOperationSupport(order = 301)
    @ApiImplicitParam(name = "ownerId", value = "业主id", required = true)
    public JsonResult<Void> disEnable(@PathVariable("ownerId") @Min(1) Long ownerId){
        ownerModuleService.ownerDisEnable(ownerId);
        return JsonResult.ok();
    }

    @PutMapping("/updateOwner")
    @ApiOperation("修改业主信息")
    @ApiOperationSupport(order = 310)
    public JsonResult<Void> updateOwnerInfo(@Valid OwnerUpdateModuleDTO dto){
        ownerModuleService.updateOwnerInfo(dto);
        return JsonResult.ok();
    }


    @GetMapping("")
    @ApiOperation("获取业主列表模块")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<OwnerListModuleVO>> list() {
        List<OwnerListModuleVO> ownerModuleList = ownerModuleService.getOwnerModuleList();
        return JsonResult.ok(ownerModuleList);
    }

    @GetMapping("{ownerId}/{houseId}/{carPortNumber}")
    @ApiOperation("获取业主信息")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "ownerId", value = "业主id", required = true),
                    @ApiImplicitParam(name = "houseId", value = "房产id", required = true),
                    @ApiImplicitParam(name = "carPortNumber", value = "车位编号")
            }
    )
    public JsonResult<OwnerStandardModuleVO> owner(@PathVariable("ownerId") @Min(1) Long ownerId,
                                                   @PathVariable("houseId") @Min(1) Long houseId,
                                                   @PathVariable("carPortNumber") String carPortNumber) {
        OwnerStandardModuleVO ownerModuleStandardById = ownerModuleService.getOwnerModuleStandardById(ownerId, houseId, carPortNumber);
        return JsonResult.ok(ownerModuleStandardById);
    }

    @GetMapping("{ownerId}/{houseId}/")
    @ApiOperation("获取业主信息无车位")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "ownerId", value = "业主id", required = true),
                    @ApiImplicitParam(name = "houseId", value = "房产id", required = true),

            }
    )
    public JsonResult<OwnerStandardModuleVO> owner(@PathVariable("ownerId") @Min(1) Long ownerId,
                                                   @PathVariable("houseId") @Min(1) Long houseId) {
        return owner(ownerId, houseId, null);
    }




}
